package com.hcloud.uploadsdk;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.cloud.wolibrary.bean.UploadProgress;
import com.cloud.wolibrary.callback.IUploadResultCallback;
import com.cloud.wolibrary.error.ServerError;
import com.cloud.wolibrary.manager.UploadManager;
import com.cloud.wolibrary.request.PutUploadRequest;
import com.cloud.wolibrary.request.UploadRequest;

import java.util.List;

public class UploadAdapter extends RecyclerView.Adapter<UploadAdapter.UploadHolder> {

    private List<Upload> uploadTaskList;

    private UploadManager manager;
    private UploadActivity activity;

    @NonNull
    @Override
    public UploadHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file, null);

        return new UploadHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull UploadHolder holder, int position) {
        holder.setData(uploadTaskList.get(position));
    }

    @Override
    public int getItemCount() {
        return uploadTaskList.size();
    }

    public void setData(List<Upload> list) {
        this.uploadTaskList = list;
        notifyDataSetChanged();
    }

    public void setManager(UploadManager uploadManager) {
        this.manager = uploadManager;
    }

    public void setAc(UploadActivity activity) {
        this.activity = activity;
    }

    class UploadHolder extends RecyclerView.ViewHolder {

        private Upload upload;

        private TextView tvName;
        private Button btStatus;
        private TextView tvProgress;
        private TextView tvCurrentSize;
        private TextView tvSpeed;
        private TextView tvStatus;


        public UploadHolder(View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.tv_name);
            btStatus = itemView.findViewById(R.id.bt_status);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvProgress = itemView.findViewById(R.id.tv_progress);
            tvCurrentSize = itemView.findViewById(R.id.tv_current_size);
            tvSpeed = itemView.findViewById(R.id.tv_speed);


            btStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //0 等待 1 上传中 2 暂停 3 上传成功 4 上传失败 5文件受限
                    int uploadStatus = upload.getUploadRequest().getUploadStatus();
//                    WoUtils.d("----uploadStatus----" + uploadStatus);
                    if (uploadStatus == 0 || uploadStatus == 1) {
                        if (upload.getUploadRequest() != null) {
                            upload.getUploadRequest().setUploadStatus(2);
                            upload.getUploadRequest().cancel();
                            btStatus.setText("开始");
                            tvStatus.setText("暂停上传");

                        }
                    } else {
                        if (manager != null) {

                            manager.upload((PutUploadRequest) upload.getUploadRequest());
                            btStatus.setText("暂停");
                            tvStatus.setText("上传中");
                        }
                    }
                }
            });
        }


        public void setData(Upload upload) {
            this.upload = upload;

            int uploadStatus = upload.getUploadRequest().getUploadStatus();

            if (uploadStatus == 0) {
                btStatus.setText("暂停");
                tvStatus.setText("等待上传");
            } else if (uploadStatus == 1) {
                btStatus.setText("暂停");
                tvStatus.setText("上传中");
            } else if (uploadStatus == 2) {
                btStatus.setText("开始");
                tvStatus.setText("暂停上传");
            } else if (uploadStatus == 3) {
                btStatus.setText("开始");
                tvStatus.setText("上传成功");
            } else if (uploadStatus == 4) {
                btStatus.setText("开始");
                tvStatus.setText("上传失败");
            }else if (uploadStatus == 5){
                btStatus.setText("开始");
                tvStatus.setText("文件受限");
            }

            notifyItemData(upload.getUploadRequest().getUploadProgress());

            this.upload.getUploadRequest().setUploadResultCallback(resultCallback);
        }


        private IUploadResultCallback resultCallback = new IUploadResultCallback() {
            @Override
            public void uploadProgress(UploadRequest uploadRequest, UploadProgress uploadProgress) {
                uploadRequest.setUploadStatus(1);
                if (!upload.getUploadRequest().getTag().equals(uploadRequest.getTag())) {
                    return;
                }
                tvStatus.setText("上传中");
                notifyItemData(uploadProgress);
            }

            @Override
            public void uploadSuccess(UploadRequest uploadRequest, UploadProgress uploadProgress, String fid) {
                long endTime = System.currentTimeMillis();
                System.out.println("-----endTime: " + endTime);
                System.out.println("-----共耗时: " + (endTime - activity.startTime));

                uploadRequest.setUploadStatus(3);
                if (!upload.getUploadRequest().getTag().equals(uploadRequest.getTag())) {
                    return;
                }
                notifyItemData(uploadProgress);
                tvStatus.setText("上传成功");
                btStatus.setText("开始");
            }

            @Override
            public void uploadError(UploadRequest uploadRequest, String errorCode, String errorMsg) {
                Log.i("wolib", "------" + errorCode + " " + errorMsg);
                uploadRequest.setUploadStatus(4);
                if (!upload.getUploadRequest().getTag().equals(uploadRequest.getTag())) {
                    return;
                }
                if (errorCode.equals(ServerError.UPLOAD_ERROR_10010)){
                    uploadRequest.setUploadStatus(5);
                    tvStatus.setText("文件受限");
                    btStatus.setText("开始");
                    notifyItemData(uploadRequest.getUploadProgress());
                    return;
                }

                if (errorCode.equals(ServerError.UPLOAD_ERROR_10005)) {
                    uploadRequest.setUploadStatus(2);
                    tvStatus.setText("暂停上传");
                    return;
                }
                btStatus.setText("开始");
                tvStatus.setText("上传失败 : " + errorCode + " uploadId: " + uploadRequest.getFileBean().getUploadId());
            }
        };

        private void notifyItemData(UploadProgress uploadProgress) {
            String total = ConvertUtils.byte2FitMemorySize(uploadProgress.getTotal());
            String current = ConvertUtils.byte2FitMemorySize(uploadProgress.getCurrentSize());
            String speed = ConvertUtils.byte2FitMemorySize(uploadProgress.getSpeed());
            tvName.setText(uploadProgress.getFileName());
            tvProgress.setText(uploadProgress.getProgress() + "%");
            tvCurrentSize.setText(current + " / " + total);
            tvSpeed.setText(speed + " / s");
        }
    }
}
